package com.fkframe.codegener.ui.view;

import heip.generator.entity.EntityTable;
import heip.generator.generer.EntityGenerator;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;

import cn.org.rapid_framework.generator.GeneratorProperties;

import com.fkframe.codegener.ui.EETSoftFrame;

/**
 * 实体生成
 */
public class EntityGenerView extends JPanel implements ActionListener {
	private JTextField txtModule;
	private JTextField txtTable;
	//private JCheckBox chkSequece;
	private JCheckBox chkPojoVO;
	private JCheckBox chkTempVO;
	private JComboBox selIdType;//Id 生成机制
    private JButton btnGener;
	
	private EETSoftFrame parentFrame;
	
	/**
	 * Create the panel.
	 */
	public EntityGenerView(EETSoftFrame parent) {
		parentFrame = parent;
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new GridLayout(0, 2, 3, 3));
		
		JLabel lblNewLabel = new JLabel("模块名*");
		panel.add(lblNewLabel);
		
		txtModule = new JTextField();
		panel.add(txtModule);
		txtModule.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("映射表名*");
		panel.add(lblNewLabel_2);
		
		txtTable = new JTextField();
		panel.add(txtTable);
		txtTable.setColumns(10);
		
		//chkSequece = new JCheckBox("使用递增序列号*",true);
		//panel.add(chkSequece);
        Vector<String> types = new Vector<String>();
        for(EntityGenerator.GenerationType type :EntityGenerator.GenerationType.values()){
            types.add(type.toString());
        }
        selIdType = new JComboBox(types);
        panel.add(selIdType);
		
		chkPojoVO = new JCheckBox("生成普通VO",false);
		panel.add(chkPojoVO);
		
		chkTempVO = new JCheckBox("生成临时表VO",false);
		panel.add(chkTempVO);
		
		
		btnGener = new JButton("生成代码");
		add(btnGener);
		btnGener.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		btnGener.setEnabled(false);
		
		String moduleName=txtModule.getText();
		String tableName= txtTable.getText();
		//boolean  seqFlg = this.chkSequece.isSelected();
        EntityGenerator.GenerationType idType =
                EntityGenerator.GenerationType.valueOf(this.selIdType.getSelectedItem().toString());
        boolean commonVo = this.chkPojoVO.isSelected();
		boolean temFlag = this.chkTempVO.isSelected();
		if(commonVo&&temFlag){
			JOptionPane.showMessageDialog(parentFrame, "VO只能生成一种");
			btnGener.setEnabled(true);
			return;
		}
		//System.out.println(moduleName+" "+ tableName+" "+ seqFlg+" "+commonVo+" "+temFlag);
		
		
		//--------------
		/**
		 * 注意：如果VO继承临时表需要指定外键字段
		 * 一张表一张表生成，不用设置父子关系
		 */
		try{
		EntityTable table = new EntityTable(tableName);	//参数为 表名：tc_pick_after,子表对象：sub
		EntityGenerator eg = new EntityGenerator(moduleName);
		eg.generatorPO(new EntityTable[]{table}, idType);
		if(commonVo||temFlag){
			eg.generatorVO(new EntityTable[]{table}, temFlag);
		}
		
		Runtime.getRuntime().exec("cmd.exe /c start "
				+ GeneratorProperties.getRequiredProperty("outRoot"));
		}catch(Exception ee){
			JOptionPane.showMessageDialog(parentFrame, "生成代码报错："+ee.getMessage());
			btnGener.setEnabled(true);
			return;
		}
		//--------------
		
		btnGener.setEnabled(true);
	}
	
	
}